package cloud.take.off.service.impl;

import cloud.take.off.mapper.ProductInfoMapper;
import cloud.take.off.module.common.CommonCode;
import cloud.take.off.module.common.ListResult;
import cloud.take.off.module.product.dto.ProductInfoDTO;
import cloud.take.off.module.product.dto.ProductInventoryDTO;
import cloud.take.off.module.product.entity.ProductInfo;
import cloud.take.off.module.product.entity.ProductInventory;
import cloud.take.off.module.product.vo.ProductInfoVO;
import cloud.take.off.module.product.vo.ProductInventoryVO;
import cloud.take.off.mybatis.base.BaseServiceImpl;
import cloud.take.off.mybatis.search.SearchEntity;
import cloud.take.off.mybatis.search.SearchParamEntiy;
import cloud.take.off.mybatis.support.Condition;
import cloud.take.off.mybatis.support.Query;
import cloud.take.off.service.IProductInfoService;
import cloud.take.off.wrapper.ProductInfoWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @Description: TODO
 * @Author: Take-off
 * @Date: 2024/10/14 4:39 PM
 * @Param
 * @return
 * @Version: 1.0
 **/
@Service
@AllArgsConstructor
public class ProductInfoServiceImpl extends BaseServiceImpl<ProductInfoMapper,ProductInfo> implements IProductInfoService {

    private final ProductInfoWrapper productInfoWrapper;

    @Override
    public boolean submit(ProductInfoVO productInfoVO){
        ProductInfo productInfo = productInfoWrapper.voToEntity(productInfoVO);
        boolean result = saveOrUpdate(productInfo);
        return result;
    }

    @Override
    public ProductInfoVO detail(ProductInfoDTO dto){
        ProductInfoVO infoVO = new ProductInfoVO();
        ProductInfo productInfo = getOne(Condition.getQueryWrapper(productInfoWrapper.dtoToEntity(dto)));
        if (Optional.ofNullable(productInfo).isPresent()){
            infoVO = productInfoWrapper.entityToVO(productInfo);
        }
        return infoVO;
    }

    @Override
    public ListResult<ProductInfoVO> list(Query query) {
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs()==null&&query.getDescs()==null){
            query.setDescs("create_time");
        }
        QueryWrapper<ProductInfo> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new ProductInfo()), entityList
        );
        IPage<ProductInfo> pages = page(Condition.getPage(query), queryWrapper);
        List<ProductInfoVO> productInfoVOS = productInfoWrapper.entityToVO(pages.getRecords());
        ListResult<ProductInfoVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(productInfoVOS);
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
        return listResult;
    }

}
